//
//  SelectDateView.m
//  YaDiuDiu
//
//  Created by 陈刚 on 2017/9/24.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "SelectDateView.h"

@interface SelectDateView ()<UIPickerViewDataSource,UIPickerViewDelegate>

@property (nonatomic, strong) UIView *bgView;

@property (nonatomic, strong) UIView *contentDateView;

@property (nonatomic, strong) UIPickerView *datePick;

@property (nonatomic, strong) NSMutableArray *dateArray;

@property (nonatomic, strong) UIButton *okButton;

@property (nonatomic, strong) UIButton *cancelButton;

@property (nonatomic, strong) UIView *topLine;

@property (nonatomic, strong) UIView *bottomLine;

// date

@property (nonatomic, strong) NSString *dateString;

@end

@implementation SelectDateView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setupView];
        
        [self addConstraint];
        
        [self loadData];
    }
    return self;
}

- (void)setupView{
    [self setFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    [self setBackgroundColor:[UIColor clearColor]];
    
    UITapGestureRecognizer *gest = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(cancelEvent)];
    
    self.bgView = [[UIView alloc]init];
    [self.bgView setAlpha:0.5];
    [self.bgView setBackgroundColor:[UIColor blackColor]];
    [self.bgView addGestureRecognizer:gest];
    [self addSubview:self.bgView];
    
    self.contentDateView = [[UIView alloc]init];
    [self.contentDateView setBackgroundColor:[UIColor whiteColor]];
    [self addSubview:self.contentDateView];
    
    self.datePick = [[UIPickerView alloc]init];
    [self.datePick setBackgroundColor:[UIColor whiteColor]];
    [self.datePick setDelegate:self];
    [self.datePick setDataSource:self];
    [self.contentDateView addSubview:self.datePick];
    
    self.bottomLine = [[UIView alloc]init];
    [self.bottomLine setBackgroundColor:HexColorInt32_t(e5e5e5)];
    [self.contentDateView addSubview:self.bottomLine];
    
    self.cancelButton = [[UIButton alloc]init];
    [self.cancelButton.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [self.cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [self.cancelButton setTitleColor:HexColorInt32_t(e84515) forState:UIControlStateNormal];
    [self.cancelButton addTarget:self action:@selector(cancelEvent:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentDateView addSubview:self.cancelButton];
    
    self.okButton = [[UIButton alloc]init];
    [self.okButton.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [self.okButton setTitle:@"确定" forState:UIControlStateNormal];
    [self.okButton setTitleColor:HexColorInt32_t(e84515) forState:UIControlStateNormal];
    [self.okButton addTarget:self action:@selector(okEvent:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentDateView addSubview:self.okButton];
    
    self.topLine = [[UIView alloc]init];
    [self.topLine setBackgroundColor:HexColorInt32_t(e5e5e5)];
    [self.contentDateView addSubview:self.topLine];
}

- (void)addConstraint{
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    [self.contentDateView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(@(0));
        make.left.equalTo(@(0));
        make.right.equalTo(@(0));
        make.height.equalTo(@(244));
    }];
    
    [self.datePick mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(@(0));
        make.left.equalTo(@(0));
        make.right.equalTo(@(0));
        make.height.equalTo(@(200));
    }];
    
    [self.bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(@(-200));
        make.left.equalTo(@(0));
        make.right.equalTo(@(0));
        make.height.equalTo(@(1));
    }];
    
    [self.cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(@(-200));
        make.left.equalTo(@(0));
        make.width.equalTo(@(45));
        make.height.equalTo(@(44));
    }];
    
    [self.okButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(@(-200));
        make.right.equalTo(@(0));
        make.width.equalTo(@(45));
        make.height.equalTo(@(44));
    }];
    
    [self.topLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(@(-244));
        make.left.equalTo(@(0));
        make.right.equalTo(@(0));
        make.height.equalTo(@(1));
    }];
}

#pragma mark ====== UIPickerViewDataSource

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return self.dateArray.count;
}

- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return [NSString stringWithFormat:@"%@年",[self.dateArray objectAtIndex:row]];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    self.dateString = [NSString stringWithFormat:@"%@年",[self.dateArray objectAtIndex:row]];
    
}

- (void)loadData{
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    // 获取当前日期
    NSDate* dt = [NSDate date];
    // 定义一个时间字段的旗标，指定将会获取指定年、月、日、时、分、秒的信息
//    unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond | NSCalendarUnitWeekday;
    // 获取不同时间字段的信息
    NSDateComponents* comp = [gregorian components: NSCalendarUnitYear fromDate:dt]; // 获取各时间字段的数值
    NSLog(@"现在是%ld年" , comp.year);
    NSInteger count = comp.year - 2016;
    for (int i = 0; i < count; i ++) {
        [self.dateArray addObject:[NSString stringWithFormat:@"%zd",2017 + i]];
    }
    [self.datePick reloadAllComponents];
}

- (void)showDate{
    [KWindow addSubview:self];
}

- (void)cancelEvent{
    [self removeFromSuperview];
}

- (void)okEvent:(UIButton *)sender{
    if (self.dateString == nil) {
        self.dateString = [NSString stringWithFormat:@"%@年",[self.dateArray objectAtIndex:0]];
    }
    self.datePickSelect(self.dateString);
    [self removeFromSuperview];
}

- (void)cancelEvent:(UIButton *)sender{
    [self removeFromSuperview];
}

- (NSMutableArray *)dateArray{
    if (!_dateArray) {
        _dateArray = [[NSMutableArray alloc]init];
    }
    return _dateArray;
}

@end
